<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9/009
 * Time: 8:15
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class CubDetails extends Controller
{

    public function index()//显示俱乐部详情方法
    {
        $this->checkLogin();
        //获取俱乐部列表
        $session=Session::get('admin');
        $cub = Db::table('cub')
        ->where('adminname',$session['adminname'])
        ->paginate(10);
        // dump($cub);
        $count = count($cub);    //获取俱乐部条数
        $this->assign('list', $cub);
        $this->assign('count', $count);
        return $this->fetch();
    }

    public function edit()//显示修改页面方法
    {
        $this->checkLogin();
        $cub_id =input('cub_id');
        $sql=Db::table('cub')->where('cub_id',$cub_id)->find();
        $this->assign('result',$sql);
        return $this->fetch('edit');

    }

    public function update()//处理修改页面方法
    {
        $this->checkLogin();
        $request=input('');

        $pic= request()->file('new_pic');
        if (!is_null($pic)){
            //删除原来图片
            $old_pic=$request['cub_pic'];
            $file=ROOT_PATH."public".DS."static".DS. 'admin'.DS.'img'.DS.$old_pic;
            if (file_exists($file)) {
                unlink($file);//删除文件
            }
            //文件上传
            $info = $pic->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'.DS.'img');
            //dump($file);exit;
            if($info){
                // 成功上传后 获取上传信息
                $pic=$info->getFilename();
                $cub_id=$request['cub_id'];
                // dump($request);
                // foreach($request as $k=>$v) {
                //         if('new_pic' == $v) unset($request[$k]);
                //         if('cub_id' == $v) unset($request[$k]);
                // }
                // dump($request);exit();
                $request['cub_pic']=$pic;
                $sql=Db::table('cub')->where('cub_id',$cub_id)->update($request);
                if ($sql){
                  return $this->success('修改成功','admin/CubDetails/index');
                }
                else{
                    return $this->error('修改失败','admin/CubDetails/index');
                }
            }else{
                // 上传失败获取错误信息
                echo $info->getError();
            }

        }
        else{
            //没有上传文件
            $cub_id=$request['cub_id'];
            foreach( $request as $k=>$v) {
                if('new_pic' == $v) unset($request[$k]);
                if('cub_id' == $v) unset($request[$k]);
            }
            $sql=Db::table('cub')->where('cub_id',$cub_id)->update($request);
            if ($sql){
                return $this->success('修改成功','admin/CubDetails/index');
            }
            else{
                return $this->error('修改失败','admin/CubDetails/index');
            }
        }
    }

    public function search () {
        $this->checkLogin();
        $session=Session::get('admin');
        $parm = input('content');
        $cub = Db::table('cub')
        ->where('adminname',$session['adminname'])
        ->where('cub_name','like','%'.$parm.'%')
        ->paginate(10);;
        $count = count($cub);    //获取俱乐部条数
        $this->assign('list', $cub);
        $this->assign('count', $count);
        return $this->fetch('index');
    }
}



